package com.zclcs.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2生成api
 * 
 * @author 周成龙 http://localhost:9090/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

	/**
	 * 全局参数(如header中的token)
	 *
	 * @return List<Parameter>
	 */
	private List<Parameter> parameter() {
		List<Parameter> params = new ArrayList<>();
		params.add(new ParameterBuilder().name("token").description("请求令牌").modelRef(new ModelRef("string"))
				.parameterType("header").required(false).build());
		return params;
	}

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.zclcs")).paths(PathSelectors.any()).build()
				.globalOperationParameters(parameter());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("vue demo api").version("1.0").build();
	}

}
